//utf-8

//选择合约
function toAddContract(){
	var dialogStruct={
			'display':contextRootPath+'/tCMmContract/prepareExecute/toSelectContract',
			'width':720,
			'height':600,
			'title':'选择合约',
			'isIframe':'false',
			'buttons':[
			           	{'text':'确定','action':doAddFromContractSure,'isClose':true},
  						{'text':'关闭','isClose':true}
			           ]
		};
		
		var dialogAdd =jyDialog(dialogStruct).open();

	
}

//增加债权
function toAddDataCredit(){
	
	var value = $('#dtocooperorgCode').val();
	var redemptionDate = $('#redemptionDate').val();
	
	if(value==""|| null ==value){
		alert("请选择载入合约信息！");
		return;
	}
	
	var creditPackageName = $('#dealPackage').val();
	var redemptionDate = $('#redemptionDate').val();
	
	if(value!=null && value!=""){
		if(null==creditPackageName ||""==creditPackageName){
			alert("请选择包名！");
			return;
		}
		
		if(null==redemptionDate || ""==redemptionDate){
			alert("请选择赎回日期！");
			return;
		}
	}
	
	var textValue = $("#creditPackage option[value="+creditPackageName+"]").length;
	
	if(textValue<=0){
		alert("包名在选项列表中不存在！");
		return;
	}
	
	var dialogStruct={
			'display':contextRootPath+'/tCMmCreditDetail/prepareExecute/toAddRedempCredit',
			'width':900,
			'height':600,
			'title':'债权列表',
			'isIframe':'false',
			'buttons':[
			         {'text':'确定','action':doAddCredit,'isClose':true},
			  		 {'text':'关闭','isClose':true}
			]
		};
		
		var dialogAdd =jyDialog(dialogStruct).open();
}

//确认债权
function doAddCredit(obj){
	
	var v_ids = iframeCredit.iframeObj["table"].getSelectedIds().join(",");
	//如果没有选中 数据则
	if(v_ids == ""){
		alert("请选择债权!");
		return;
	}
	
	
	var table=iframeCredit.iframeObj["table"];
	var transferIds = table.getSelectedObjs("transferId");
	var dtocooperorgCode = $('#dtocooperorgCode').val();
	var dtoagreementCode = $('#dtoagreementCode').val();
	var valueContractCode = $('#dtocontractCode').val();
	transferIds = "" + transferIds;
	var thisMakeCreditorValues = table.getSelectedObjs("thisMakeCreditorValue");//thisMakeCreditorValue
	thisMakeCreditorValues = "" + thisMakeCreditorValues;
	var ids = table.getSelectedObjs("id");
	ids = "" + ids;
	var dealCode = $('#dealCode').val();
	var redemptionDate = $('#redemptionDate').val();
	var id = $('#idValue').val();
	
	var creditPackageName = $('#dealPackage').val();
	var dataUrl =contextRootPath + "/creditRedem/updateCreditForRedemption";
	
	var sumCreditorValue = table.getSelectedObjs("thisMakeCreditorValue").join(",").split(",");
	
	var i;
	for(i=0;i<sumCreditorValue.length;i++){
		var creditValue = sumCreditorValue[i];
		if(creditValue=="0"){
			alert("所选日期债权赎回价值为零，不能赎回！");
			return;
		}
	}
		
	$.ajax({
		url: dataUrl,
		type: 'post',
		dataType: 'json',
		data:{"transferIds":transferIds,
			"thisMakeCreditorValues":thisMakeCreditorValues,
			"ids":ids,
			"dealCode":dealCode,
			"id":id,
			"redeemDate":redemptionDate,
			"contractCode":valueContractCode,
			"packageNum":creditPackageName,
			"cooperorgCode":dtocooperorgCode,
			"agreementCode":dtoagreementCode},
		success: function(results) {
			var obj = results.data;
			$("#idValue").val(obj.id);
			$("#dealCode").val(obj.dealCode);
			$("#dealDate").val(obj.dealDate);
			initFnCreditDetailView(transferIds);
			$("#creditDetailTable").show();
			CreditValueSum();
//			$("#isDeleteAndIsExport").val("true");
			alert(results.msg);
			
		}
	});
}

//债权价值合计
function CreditValueSum(){
	var dealCode = $("#dealCode").val();
	var dataUrl =contextRootPath + "/cooperorgDeal/getDealDetailByCreditSumTotal";	
	$.ajax({
		url: dataUrl,
		type: 'post',
		dataType: 'json',
		data:{"dealCode":dealCode},
		error: function(result) {
			alert("请查看返回数据是否正确：" + result);
		},
		success: function(results) {
			var obj = results.data;
			$("#detialNumber").val(obj.detialNumber);
			$("#sumCreditorValue").val(obj.sumThisMakeCreditorValue);
			$("#sumCautionMoney").val(obj.sumCautionMoney);
			$("#sumMakeCreditorValue").val(obj.sumMakeCreditorValue);
		}
	});
}

//确认合约
function doAddFromContractSure(){
	var contractCode = iframeSelectContract.iframeObj["table"].getSelectedObjs("contractCode");	
	 window.location = contextRootPath+'/creditRedem/prepareExecute/toBackRedemptionAdd?contractCode='+contractCode;
	
}

//双击合约
function doAddFromContract(contractCode){
	 window.location = contextRootPath+'/creditRedem/prepareExecute/toBackRedemptionAdd?contractCode='+contractCode;
	
}

//查询债权包隐藏标签
var selectOptions=[];
function searchCreditPackage(selectID,updatePackageValue,type){
	var value = $('#dtocooperorgCode').val();
	if(value==""|| null ==value){
		return;
	}
	
	var valueCooperorgCode = $('#dtocooperorgCode').val();
	var contractCode = $('#dtocontractCode').val();
	var dataUrl = contextRootPath + '/cooperorgDeal/getPackageList?cooperorgCode=' + valueCooperorgCode + "&contractCode=" + contractCode + "&pagType=" + cimsConstant.CREDIT_RESOURCE;
	$.ajax({
		url: dataUrl,
		type: 'get',
		dataType: 'json',
		data:'',
		error: function(result) {
			alert("请查看返回数据是否正确：" + result);
		},
		success: function(results) {
			var result=results.data;
			var j;
			var resultLength = result.length - 1;
			for (j = 0; j <= resultLength; j++) {
				$('#creditPackage').append("<option value='"+result[j].creditPackageName+"'>"+result[j].creditPackageName+"</option>");
				selectOptions.push(result[j].creditPackageName);
			}
			completePackage(selectOptions,selectID,updatePackageValue,type);
		}
	});

}

//保存交易流水
function saveDeal(){
	
	CreditValueSum();
	
	var valueCooperorgCode = $('#dtocooperorgCode').val();
	var valueContractCode = $('#dtocontractCode').val();
	var creditPackageName = $('#dealPackage').val();
	var dealCode = $('#dealCode').val();
	var dealDate = $('#dealDate').val();
	var id = $('#idValue').val();
	var sumCreditorValue = $('#sumCreditorValue').val();
	var detialNumber = $('#detialNumber').val();
	var dealType = $('#dealType').val();
	var dtoagreementCode = $('#dtoagreementCode').val();
	var redemptionDate = $('#redemptionDate').val();
	var sumCautionMoney = $('#sumCautionMoney').val();
	var sumMakeCreditorValue = $('#sumMakeCreditorValue').val();
	if(null==valueCooperorgCode||""==valueCooperorgCode){
		alert("请选择合约！");
		return;
	}
	
	if(null==dealCode||""==dealCode){
		alert("请选择债权！");
		return;
	}
	
	if(confirm("确认保存！")){
		var dataUrl =contextRootPath + "/creditRedem/saveRedemptionDeal";
		$.ajax({
			url: dataUrl,
			type: 'post',
			dataType: 'json',
			data:{"cooperorgCode":valueCooperorgCode,
				  "contractCode":valueContractCode,
				  "creditPackageName":creditPackageName,
				  "dealCode":dealCode,
				  "id":id,
				  "sumCreditorValue":sumCreditorValue,
				  "cautionMoney":sumCautionMoney,
				  "practicalSellSum":sumMakeCreditorValue,
				  "detialNumber":detialNumber,
				  "dealType":dealType,
				  "agreementCode":dtoagreementCode,
				  "redemptionDate":redemptionDate,
				  "dealDate":dealDate},
			error: function(result) {
				alert("请查看返回数据是否正确：" + result);
			},
			success: function(results) {
				$("#isDeleteAndIsExport").val("true");
				var winObj = parent.tabs.getTabWinByTitle("债权赎回");
				if(winObj)
				winObj.location.reload();
			}
		});
	}
}

//保存验证是否存在债权
function saveCreditDeal(){
	
	var dealCode = $('#dealCode').val();
	
	if(null==dealCode ||""==dealCode){
		alert("请选择债权！");
		return;
	}
	
	var dataUrl =contextRootPath + "/tCMmCreditDealDetail/queryListTCMmCreditDealDetail?dealStreamCode=" + dealCode;
	$.ajax({
		url: dataUrl,
		type: 'get',
		dataType: 'json',
		data:'',
		success: function(results) {
			var result=results.data;
			var resultLength = result.length;
			if(resultLength<=0){
				alert("该交易下没有债权！");
			}else {
				saveDeal();
			}
		}
	});
}

//试算
function settleCreditDeal(){
	var dealCode = $('#dealCode').val();
	
	if(null==dealCode||""==dealCode){
		alert("请先导入债权确认信息！");
		return;
	}
	
	if(confirm("请确认导入的债权信息!")){
		var dataUrl =contextRootPath + "/settle/testSettleCooperorgDeal";
		$.ajax({
			url: dataUrl,
			type: 'post',
			dataType: 'json',
			data:{"dealCode":dealCode},
			error: function(result) {
				alert("请查看返回数据是否正确：" + result);
			},
			success: function(results) {
				
			}
		});
	}
}

//债权包输入
function creditPackageOperation(obj){
	
	
	
}

//删除债权
function deleteCredit(){
	
	var table=iframeCreditView.iframeObj["table"];
	var transferIds = table.getSelectedObjs("transferId");
	transferIds = transferIds + "";
	var v_ids = iframeCreditView.iframeObj["table"].getSelectedIds().join(",");
	var dealCode = $('#dealCode').val();
	//如果没有选中 数据则
	if(v_ids == ""){
		alert("请选择待删除的数据！");
		return;
	}
	if(confirm("您确认要删除选择的数据吗？")){
        $.ajax({
            type:"POST",
            dataType : "json",
            url:contextRootPath+"/creditRedem/deleteDetailByCredit",
            data:{"transferIds":transferIds
            	  ,"dealCode":dealCode},
            success:function(msg){
            	alert(msg.msg);
            	var v_status = msg.status;
            	//删除成功后
            	if(v_status.indexOf('ok') >-1){
            		iframeCreditView.iframeObj["table"].removeSelectedRow();
            		CreditValueSum();
            	}
            	
            	
            }
        });
    }
}

/**
 * 债权赎回通知
 */
function doFinish(){
	var dealCode = $('#dealCode').val(); //交易流水号
	var valueCooperorgCode = $('#dtocooperorgCode').val();//转让机构编号
	
	if(null==valueCooperorgCode ||""==valueCooperorgCode){
		alert("请选择合作机构！");
		return;
	}
	if(dealCode==""){
		alert("请先点击保存！");
	}
	var dataUrl =contextRootPath + "/creditRedem/doNoticeCredit";
	$.ajax({
		url: dataUrl,
		type: 'post',
		dataType: 'json',
		data:{"cooperorgCode":valueCooperorgCode,"dealCode":dealCode},
		success: function(results) {
			if(results.status=="failed"){
				alert("发送完成失败！"+results.msg);
			}else{
				alert(results.msg);
			}
		}
	});
}

